<html>
<head><meta charset="utf-8"><title>Macro eager expansion? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html">Macro eager expansion?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="247654937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/247654937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#247654937">(Jul 29 2021 at 23:36)</a>:</h4>
<p>Hi all,</p>
<p>I got another question about procedure_macro_attribute.</p>
<p>Suppose I have this attribute:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[attribute(module_path!())]</span><span class="w"></span>
<span class="c1">// a rust item</span>
</code></pre></div>
<p>What I am trying to do here is that I want to get the module path of the code at call site and use that in my attribute:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// the path should be the module path of the src which calls the attribute</span>
<span class="cp">#[attribute(crate::mod::name)]</span><span class="w"></span>
<span class="c1">// rust item</span>
</code></pre></div>
<p>However, I don't know if there's anyway to force an eager expansion of <code>module_path!()</code>, currently my attribute cannot handle <code>module_path!()</code> as input, it can only handle a valid rust path.</p>
<p>How should I do that? Or is there any workaround to get the module path of the call site of proc_macro?</p>



<a name="247697091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/247697091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#247697091">(Jul 30 2021 at 11:45)</a>:</h4>
<p>I don't think it is possible to do this with proc macros. Where do you need the module path for? If it is as string, you can use emit a <code>module_path!()</code> invocation with the call site span inside your macro output. If you need it as path, maybe <code>self::</code> or <code>super::</code> or something like that works?</p>



<a name="247732777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/247732777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#247732777">(Jul 30 2021 at 16:42)</a>:</h4>
<blockquote>
<p>If you need it as path, maybe <code>self::</code> or <code>super::</code> or something like that works?</p>
</blockquote>
<p>Yes I need it as path so that they can be processed in my attribute, the problem is that I need the result of expansion instead of the macro call itself. Can you explain more about how you do that with <code>self</code>?</p>



<a name="247736644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/247736644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#247736644">(Jul 30 2021 at 17:14)</a>:</h4>
<p>I thought if you needed to refer to an item within the current module, you can use those.</p>



<a name="247736658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/247736658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#247736658">(Jul 30 2021 at 17:14)</a>:</h4>
<p>What do you exactly need the full module path for?</p>



<a name="248254937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/248254937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#248254937">(Aug 03 2021 at 17:48)</a>:</h4>
<p>Full path is for a exporting feature I am developing, I would like to export the syntax defined in <code>prusti</code> and reuse it in different crate. I think it would be better if I have the full module path because the prusti features are tagged with the source code user write</p>



<a name="248255173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Macro%20eager%20expansion%3F/near/248255173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Macro.20eager.20expansion.3F.html#248255173">(Aug 03 2021 at 17:50)</a>:</h4>
<p>So if there's no way to eager expand a macro, then a full module path at the callsite of proc_macro will be very useful, do anyone know how I can get that?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>